<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <title>Client Authentication Example</title>
    <link type="text/css" rel="stylesheet"
          href="webjars/bootstrap/3.0.3/css/bootstrap.min.css"/>
    <script type="text/javascript" src="webjars/jquery/1.9.0/jquery.min.js"></script>
    <script type="text/javascript"
            src="webjars/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <script>
        /**
         * oauth2.0 ?????implicit grant type?
         */
        function authenticate() {
            var appID = "my-less-trusted-client";
            // var appID = "my-client-with-registered-redirect";
            var path = 'oauth/authorize?';
            var queryParams = ['client_id=' + appID,
                'redirect_uri=' + window.location, 'scope=trust',
                'response_type=token'];
            var query = queryParams.join('&');
            var url = path + query;
            window.location.href = url;
        }

        function display() {
            var hash = window.location.hash;
            var accessToken = hash.split('&')[0].split("=")[1];
            console.log('access-token:' + accessToken);
            var headers = {
                'Authorization': 'Bearer ' + accessToken,
                'Accept': 'application/json'
            };
            $.ajaxSetup({
                'headers': headers,
                dataType: 'text'
            });
            $.get('photos/user/message', function (data) {
                console.log('data:' + data);
                $('#message').text(data);
            });
            // $.get('photos', function(data) {
            //     console.log('data:' + data);
            //     $('#message').text(data);
            // });
        }

        $(function () {
            if (window.location.hash.length == 0) {
                authenticate();
            } else {
                display();
            }
        })
    </script>
</head>
<body>
<h1>Sparklr Client Authentication Sample</h1>

<div id="content">
    <p>Once you have authenticated and approved the access, some
        JavaScript in this page will render a message from Sparklr below</p>
    <p id="message"/>
</div>
</body>
</html>
